package com.luoxin.servicedriveruser.controller;

import com.alibaba.fastjson.JSONObject;
import com.luoxin.internalcommon.constant.DriverCarConstants;
import com.luoxin.internalcommon.dto.ResponseResult;
import com.luoxin.internalcommon.entity.DriverUser;
import com.luoxin.internalcommon.response.DriverUserExistsResponse;
import com.luoxin.servicedriveruser.service.DriverUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private DriverUserService driverUserService;

    /**
     * 新增司机
     * @param driverUser
     * @return
     */
    @PostMapping("/driver-user")
    public ResponseResult addUser(@RequestBody DriverUser driverUser){
        log.info(JSONObject.toJSONString(driverUser));
        return driverUserService.addDriverUser(driverUser);
    }


    @PutMapping("/driver-user")
    public ResponseResult updateUser(@RequestBody DriverUser driverUser){
        log.info(JSONObject.toJSONString(driverUser));
        return driverUserService.updateDriverUser(driverUser);
    }


    /**
     * 根据driverPhone查询司机
     * @return
     */
    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult<DriverUserExistsResponse> getUser(@PathVariable("driverPhone") String driverPhone){
        ResponseResult<DriverUser> userByPhone = driverUserService.getDriverUserByPhone(driverPhone);
        DriverUser driverUserDb = userByPhone.getData();
        DriverUserExistsResponse driverUserExistsResponse = new DriverUserExistsResponse();
        if (driverUserDb == null){
            driverUserExistsResponse.setIfExist(DriverCarConstants.DRIVER_NOT_EXISTS);
            driverUserExistsResponse.setDriverPhone(driverPhone);
            return ResponseResult.success(driverUserExistsResponse);
        }

        driverUserExistsResponse.setIfExist(DriverCarConstants.DRIVER_EXISTS);
        driverUserExistsResponse.setDriverPhone(driverUserDb.getDriverPhone());
        return ResponseResult.success(driverUserExistsResponse);
    }
}
